package com.heartware.sh.blackhole.ui.activity;

import android.graphics.Color;
import android.text.Editable;
import android.text.Layout;
import android.text.Selection;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextWatcher;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.heartware.sh.blackhole.R;
import com.heartware.sh.blackhole.ui.activity.base.BaseActivity;
import com.widget.FontEditText;

/**
 * Created by Administrator on 2016/12/8 0008.
 */
public class SixActivity extends BaseActivity {

    public static final String TAG = SixActivity.class.getSimpleName();

    private FontEditText et_input;
    private LinearLayout ll_temp;

    /**生产者消费者问题，涉及到几个类
     * 第一，这个问题本身就是一个类，即主类
     * 第二，既然是生产者、消费者，那么生产者类和消费者类就是必须的
     * 第三，生产什么，消费什么，所以物品类是必须的，这里是馒头类
     * 第四，既然是线程，那么就不是一对一的，也就是说不是生产一个消费一个，既然这样，多生产的往哪里放，
     *      现实中就是筐了，在计算机中也就是数据结构，筐在数据结构中最形象的就是栈了，因此还要一个栈类
     */

    @Override
    public int getLayoutId() {
        return R.layout.activity_six;
    }

    @Override
    public void initView() {
        super.initView();

        et_input = (FontEditText) findViewById(R.id.et_input);
        ll_temp = (LinearLayout) findViewById(R.id.ll_temp);


        SpannableStringBuilder builder = new SpannableStringBuilder("测试内容测试内容");

//ForegroundColorSpan 为文字前景色，BackgroundColorSpan为文字背景色
        ForegroundColorSpan redSpan = new ForegroundColorSpan(Color.RED);
        ForegroundColorSpan whiteSpan = new ForegroundColorSpan(Color.WHITE);
        ForegroundColorSpan blueSpan = new ForegroundColorSpan(Color.BLUE);
        ForegroundColorSpan greenSpan = new ForegroundColorSpan(Color.GREEN);
        ForegroundColorSpan yellowSpan = new ForegroundColorSpan(Color.YELLOW);

        builder.setSpan(redSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(whiteSpan, 1, 2, Spannable.SPAN_INCLUSIVE_INCLUSIVE);
        builder.setSpan(blueSpan, 2, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(greenSpan, 3, 4, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        builder.setSpan(yellowSpan, 4,5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);


        ((TextView)findViewById(R.id.tv)).setText(builder);



//        startThread();
    }


    @Override
    public void setListener() {
        super.setListener();


        et_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i(TAG, "========" + et_input.getSelectionStart());
                Log.i(TAG, "========光标在" + getCurrentCursorLine(et_input));

                int i = getCurrentCursorLine(et_input)>=7?7:getCurrentCursorLine(et_input);

                Log.i(TAG, "======="+s.toString());

                if(ll_temp.getVisibility() == View.VISIBLE){

                    ll_temp.layout(10, 70*i, 10+ll_temp.getWidth(), 70*i + ll_temp.getHeight());

                }

            }
        });

        ll_temp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ll_temp.setVisibility(View.GONE);
            }
        });



    }

    private int getCurrentCursorLine(EditText editText) {
            int selectionStart = Selection.getSelectionStart(editText.getText());
             Layout layout = editText.getLayout();

            if (selectionStart != -1) {
                return layout.getLineForOffset(selectionStart) + 1;
             }

            return -1;
     }

    /**
     * 启动生产者， 消费者线程
     */
    public void startThread(){

        SyncStack ss = new SyncStack();

        Producer p = new Producer(ss);
        Consume c = new Consume(ss);

        Thread t1 = new Thread(p); //生产者线程
        Thread t2 = new Thread(c); //消费者线程

        t1.start();
        t2.start();

    }

    class SteamBread{
        int id;

        SteamBread(int id){
            this.id = id;
        }

        public String toString(){
            return "SteamBread:"+id;
        }
    }

    /**
     * 容器...
     */
    class SyncStack{
        int index = 0;
        SteamBread[] stb = new SteamBread[6];  //容器容量为6;

        //放入..
        public synchronized void push(SteamBread sb){
            while (index == stb.length){ //装满了.
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notify();
            stb[index] = sb;
            this.index++;
        }


        //消费
        public synchronized SteamBread pop(){

            while(index == 0){
                try {
                    this.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            this.notify();
            this.index--;
            return stb[index];
        }
    }

    /**
     * 生产者类，实现了Runnable接口，以便于构造生产者线程
     */
    class Producer implements Runnable {

        SyncStack ss;

        Producer(SyncStack ss){
            this.ss = ss;
        }

        @Override
        public void run() {
            //开始生产馒头...
            for(int i=0; i<20; i++){
                SteamBread sb = new SteamBread(i);
                ss.push(sb);
                Log.i(TAG, "生产了: "+sb.toString());
                try {
                    Thread.sleep(10);       //每生产一个馒头, 休息10毫秒
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 消费者类，实现了Runnable接口，以便于构造消费者线程
     */
    class Consume implements Runnable{

        SyncStack ss;

        Consume(SyncStack ss){
            this.ss = ss;
        }

        @Override
        public void run() {

            //开始消费
            for (int i=0; i<20; i++){
                SteamBread sb = ss.pop();
                Log.i(TAG, "消费了: "+sb.toString());
                try {
                    Thread.sleep(100);  //每消费1个馒头, 需休息100毫秒.
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
